package cn.yunhe.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import cn.yunhe.biz.IUserLoginBiz;
import cn.yunhe.biz.impl.UserLoginBizImpl;
import cn.yunhe.model.UserInfo;

public class UserLoginServlet extends HttpServlet {
	 
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=UTF-8");
		PrintWriter out=response.getWriter();
		HttpSession session=request.getSession();
		
		String username=request.getParameter("txtusername");
		String pwd=request.getParameter("txtuserpassword");
		System.out.println(username);
		UserInfo user=new UserInfo();
		user.setUsername(username);
		user.setPassword(pwd);
		IUserLoginBiz ulb=new UserLoginBizImpl();
		
		UserInfo userreturn=ulb.userLogin(user);
		if(userreturn!=null){
			session.setAttribute("user", userreturn);
			response.sendRedirect("mainform/main.html");
		}else{
			out.print("<script>alert('登录名或密码错误');location.href='index.html';</script>");
		}		
	}

	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		doPost(req, resp);
	}
}
